// pages/orders/orders.js
import {
  ordersBehavior
} from "./behavior"
import {
  findAllOrders
} from "../../api/orders"

import dayjs from "dayjs"
Page({

  behaviors: [ordersBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    orderStatusList: [
      "制作中",
      "派送中",
      "已完成"
    ],
    ordersList: [],
    activeBar: 0
  },
  // 切换查询订单状态
  changeTopBar(event) {
    // console.log(event);
    this.setData({
      activeBar: event.currentTarget.dataset.index
    })
    this.getOrderList()
  },
  // 再来一单逻辑
  getAgainOrders(event) {
    // 获取订单下标
    const index = event.currentTarget.dataset.index
    // 获取此次订单所有的商品信息
    const orderitems = this.data.ordersList[index].orderitems
    // 修改购物车为当前订单的商品信息
    this.changeCartList(orderitems)
    //此时需要打开购物车
    this.changeShow(true)
    // 跳转到list界面
    this.toListTab()
  },
  // 跳转到list界面
  toListTab() {
    wx.switchTab({
      url: '/pages/list/list',
    })
  },

  // 跳转评论页面
  toPingLunPages(event) {
     // 获取订单下标
     const index = event.currentTarget.dataset.index
     const order = this.data.ordersList[index]
     this.updateOrder(order)
     wx.navigateTo({
       url: '/pages/pinglun/pinglun',
     })
    // console.log(order,'@@@@');
  },

  async getOrderList() {
    const {
      data: ordersList
    } = await findAllOrders(this.data.userInfo.id,this.data.orderStatusList[this.data.activeBar])
    ordersList.forEach(item => {
      item.orderitems = item.orderitems && JSON.parse(item.orderitems)
      item.ordertime = dayjs(item.ordertime).format("YYYY-MM-DD HH:mm:ss")
    })
    this.setData({
      ordersList
    })
  },

  async onShow() {
    if (this.data.userInfo.id) {
      this.getOrderList()
    }
  }
})